﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace hello_accinv
{
    public partial class Payment : Form
    {
        private Database db;
        private Decimal totalAmount;
        private Decimal totalCash;
        public int newSaleID;
        private bool invoicePrinted;

        public Payment()
        {
            InitializeComponent();
            db = new Database();
            invoicePrinted = false;
        }

        private void txtboxTotalCash_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F1:
                    ChangeSalePrice();
                    break;
                case Keys.Enter:
                    if (IsValid())
                    {
                        totalCash = Decimal.Parse(txtboxTotalCash.Text);
                        txtboxTotalCash.Text = Decimal.Parse(txtboxTotalCash.Text).ToString("#,#0.00#");
                        CalculateChanges();
                        txtboxChanges.Focus();
                    }
                    else
                    {
                        txtboxTotalCash.ForeColor = Color.Red;
                        ClearChanges();
                    }
                    break;
                case Keys.Delete:
                    txtboxTotalCash.Text = "";
                    break;
            }
        }

        private bool IsValid()
        {
            if ((txtboxTotalCash.Text.Length > 0) && 
                (IsNumeric(txtboxTotalCash.Text.Replace(",",""))) && 
                (Decimal.Parse(txtboxTotalCash.Text) > 0) &&
                (Decimal.Parse(txtboxTotalCash.Text) >= Decimal.Parse(txtboxTotalAmount.Text)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void CalculateChanges()
        {
            txtboxChanges.Text = (Decimal.Parse(txtboxTotalCash.Text) - Decimal.Parse(txtboxTotalAmount.Text)).ToString("#,#0.00#");

            decimal changes = Decimal.Parse(txtboxChanges.Text);
            Console.WriteLine(changes);

            //1000
            txtboxC1000.Text = ((int)(changes / 1000)).ToString();
            changes = changes - (Int32.Parse(txtboxC1000.Text) * 1000);
            txtboxC1000.BackColor = (txtboxC1000.Text.Equals("0")) ? Color.White : Color.LightGreen; 
                
            //500
            txtboxC500.Text = ((int)(changes / 500)).ToString();
            changes = changes - (Int32.Parse(txtboxC500.Text) * 500);
            txtboxC500.BackColor = (txtboxC500.Text.Equals("0")) ? Color.White : Color.LightGreen; 
               
            //100
            txtboxC100.Text = ((int)(changes / 100)).ToString();
            changes = changes - (Int32.Parse(txtboxC100.Text) * 100);
            txtboxC100.BackColor = (txtboxC100.Text.Equals("0")) ? Color.White : Color.LightGreen; 
                
            //50
            txtboxC50.Text = ((int)(changes / 50)).ToString();
            changes = changes - (Int32.Parse(txtboxC50.Text) * 50);
            txtboxC50.BackColor = (txtboxC50.Text.Equals("0")) ? Color.White : Color.LightGreen;     

            //20
            txtboxC20.Text = ((int)(changes / 20)).ToString();
            changes = changes - (Int32.Parse(txtboxC20.Text) * 20);
            txtboxC20.BackColor = (txtboxC20.Text.Equals("0")) ? Color.White : Color.LightGreen;     

            //10
            txtboxC10.Text = ((int)(changes / 10)).ToString();
            changes = changes - (Int32.Parse(txtboxC10.Text) * 10);
            txtboxC10.BackColor = (txtboxC10.Text.Equals("0")) ? Color.White : Color.LightGreen;     

            //5
            txtboxC5.Text = ((int)(changes / 5)).ToString();
            changes = changes - (Int32.Parse(txtboxC5.Text) * 5);
            txtboxC5.BackColor = (txtboxC5.Text.Equals("0")) ? Color.White : Color.LightGreen;     

            //1
            txtboxC1.Text = ((int)changes).ToString();
            txtboxC1.BackColor = (txtboxC1.Text.Equals("0")) ? Color.White : Color.LightGreen; 
        }

        private bool IsNumeric(string input)
        {
            string validChars = "0123456789.";
            char inputChar;
            bool result = true;
            for (int i = 0; i < input.Length; i++)
            {
                inputChar = input[i];
                if (validChars.IndexOf(inputChar) == -1)
                {
                    result = false;
                    break;
                }
            }
            return result;
        }

        private void btnChangeSalePrice_Click(object sender, EventArgs e)
        {
            ChangeSalePrice();
        }

        private void ChangeSalePrice()
        {
            txtboxTotalAmount.BackColor = Color.White;
            txtboxTotalAmount.ReadOnly = false;
            txtboxTotalAmount.Focus();
            txtboxTotalAmount.SelectAll();
            toolTip1.ShowAlways = true;
            toolTip1.Show("Enter new prices", label2);
        }

        private void txtboxTotalAmount_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (IsNumeric(txtboxTotalAmount.Text) && 
                        Decimal.Parse(txtboxTotalAmount.Text) > 0M && 
                        Decimal.Parse(txtboxTotalAmount.Text) < totalAmount)
                    {
                        txtboxTotalAmount.Text = Decimal.Parse(txtboxTotalAmount.Text).ToString("#,#0.00#");
                        label8.Text = totalAmount.ToString("#,#0.00#") + "  Baht";
                        label7.Visible = true;
                        label8.Visible = true;
                    }
                    else
                    {
                        txtboxTotalAmount.Text = totalAmount.ToString();
                    }
                    txtboxTotalAmount.BackColor = Color.LemonChiffon;
                    txtboxTotalAmount.ReadOnly = true;
                    toolTip1.Hide(label2);
                    if (txtboxTotalCash.Text.Length > 0)
                    {
                        CalculateChanges();
                        txtboxChanges.Focus();
                        txtboxChanges.SelectAll();
                    }
                    else
                    {
                        ClearChanges();
                        txtboxTotalCash.Focus();
                    }
                    break;
                case Keys.Escape:
                    txtboxTotalAmount.Text = totalAmount.ToString();
                    txtboxTotalAmount.BackColor = Color.LemonChiffon;
                    txtboxTotalAmount.ReadOnly = true;
                    toolTip1.Hide(label2);
                    if (txtboxTotalCash.Text.Length > 0)
                    {
                        CalculateChanges();
                        txtboxChanges.Focus();
                        txtboxChanges.SelectAll();
                    }
                    else
                    {
                        ClearChanges();
                        txtboxTotalCash.Focus();
                    }
                    break;
            }
        }

        private void Payment_Load(object sender, EventArgs e)
        {
            totalAmount = Decimal.Parse(txtboxTotalAmount.Text);
        }

        private void txtboxTotalCash_TextChanged(object sender, EventArgs e)
        {
            txtboxTotalCash.ForeColor = Color.Black;
        }

        private void txtboxChanges_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    FinishPayment();
                    break;
                case Keys.Escape:
                    txtboxTotalCash.Focus();
                    txtboxTotalCash.SelectAll();
                    break;
            }
        }

        private void FinishPayment()
        {
            MessageBox.Show("Finish");
            // Create sale record and send back ID to Mainform
            db.Insert("Sale", "0,'" + CurrentDateTime.GetDate() + "','" + CurrentDateTime.GetTime() + "','" + totalAmount + "','" + (totalAmount - Decimal.Parse(txtboxTotalAmount.Text)) + "','" + totalCash + "',''");
            DataTable dt = db.Select("SaleID", "Sale", "1 ORDER BY SaleID DESC LIMIT 1");
            newSaleID = Int32.Parse(dt.Rows[0]["SaleID"].ToString());

            if (!invoicePrinted)
            {
                if (MessageBox.Show("Invoice hasn't been printed yet, Print it now?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    PrintInvoice();
                }
            }

            DialogResult = System.Windows.Forms.DialogResult.OK;
            Dispose();
        }

        private void btnFinishPayment_Click(object sender, EventArgs e)
        {
            FinishPayment();
        }

        private void txtboxTotalAmount_Leave(object sender, EventArgs e)
        {
            if (!txtboxTotalAmount.ReadOnly)
            {
                txtboxTotalAmount.Text = totalAmount.ToString();
                txtboxTotalAmount.BackColor = Color.LemonChiffon;
                txtboxTotalAmount.ReadOnly = true;
                toolTip1.Hide(label2);
                if (txtboxTotalCash.Text.Length > 0)
                {
                    CalculateChanges();
                    txtboxChanges.Focus();
                    txtboxChanges.SelectAll();
                }
                else
                {
                    ClearChanges();
                    txtboxTotalCash.Focus();
                }  
            }
        }

        private void btnPrintInvoice_Click(object sender, EventArgs e)
        {
            invoicePrinted = true;
        }

        private void PrintInvoice()
        {

        }

        private void txtboxTotalCash_Enter(object sender, EventArgs e)
        {
            txtboxTotalCash.Focus();
            txtboxTotalCash.SelectAll();
        }

        private void txtboxTotalCash_MouseClick(object sender, MouseEventArgs e)
        {
            txtboxTotalCash.Focus();
            txtboxTotalCash.SelectAll();
        }

        private void txtboxTotalCash_Leave(object sender, EventArgs e)
        {
            if (IsValid())
            {
                totalCash = Decimal.Parse(txtboxTotalCash.Text);
                txtboxTotalCash.Text = Decimal.Parse(txtboxTotalCash.Text).ToString("#,#0.00#");
                CalculateChanges();
                txtboxChanges.Focus();
            }
            else
            {
                txtboxTotalCash.ForeColor = Color.Red;
                ClearChanges();
            }
        }

        private void ClearChanges()
        {
            txtboxChanges.Text = "";
            txtboxC1000.Text = "";
            txtboxC1000.BackColor = Color.White;
            txtboxC500.Text = "";
            txtboxC500.BackColor = Color.White;
            txtboxC100.Text = "";
            txtboxC100.BackColor = Color.White;
            txtboxC50.Text = "";
            txtboxC50.BackColor = Color.White;
            txtboxC20.Text = "";
            txtboxC20.BackColor = Color.White;
            txtboxC10.Text = "";
            txtboxC10.BackColor = Color.White;
            txtboxC5.Text = "";
            txtboxC5.BackColor = Color.White;
            txtboxC1.Text = "";
            txtboxC1.BackColor = Color.White;
        }
    }
}
